class BaiduMapComponent {
    constructor(containerId, options = {}) {
        this.containerId = containerId;
        this.options = options;
        this.map = null;
        this.marker = null;
        this.geocoder = null;
    }

    async init() {
        try {
            // 创建地图实例
            this.map = new BMapGL.Map(this.containerId);
            
            // 创建地理编码器
            this.geocoder = new BMapGL.Geocoder();
            
            // 初始化地图
            const point = new BMapGL.Point(116.404, 39.915); // 默认北京
            this.map.centerAndZoom(point, 15);
            
            // 开启地图控件
            this.map.enableScrollWheelZoom();
            this.map.addControl(new BMapGL.NavigationControl());
            this.map.addControl(new BMapGL.ScaleControl());
            
            // 尝试获取用户位置
            await this.getCurrentLocation();
            
            // 添加地图点击事件
            this.map.addEventListener('click', (e) => {
                // 直接使用事件中的点坐标
                const point = e.latlng;
                this.updateLocation(point);
            });
            
            // 调用初始化回调
            if (this.options.onMapInitialized) {
                this.options.onMapInitialized(this.map);
            }
        } catch (error) {
            console.error('地图初始化失败:', error);
            throw error;
        }
    }

    async getCurrentLocation() {
        return new Promise((resolve, reject) => {
            const geolocation = new BMapGL.Geolocation();
            geolocation.getCurrentPosition(
                async (r) => {
                    if (geolocation.getStatus() === BMAP_STATUS_SUCCESS) {
                        await this.updateLocation(r.point);
                        resolve(r);
                    } else {
                        console.warn('获取位置失败');
                        reject(new Error('获取位置失败'));
                    }
                },
                { enableHighAccuracy: true }
            );
        });
    }

    async updateLocation(point) {
        try {
            // 清除现有标记
            if (this.marker) {
                this.map.removeOverlay(this.marker);
            }

            // 添加新标记
            this.marker = new BMapGL.Marker(point);
            this.map.addOverlay(this.marker);
            this.map.panTo(point);

            // 解析地址
            await this.geocodeLocation(point);
        } catch (error) {
            console.error('更新位置失败:', error);
        }
    }

    async geocodeLocation(point) {
        return new Promise((resolve, reject) => {
            this.geocoder.getLocation(point, (result) => {
                if (result) {
                    const location = {
                        address: result.address,
                        latitude: point.lat,
                        longitude: point.lng,
                        province: result.addressComponents.province,
                        city: result.addressComponents.city,
                        district: result.addressComponents.district,
                        street: result.addressComponents.street,
                        streetNumber: result.addressComponents.streetNumber
                    };

                    if (this.options.onLocationSelected) {
                        this.options.onLocationSelected(location);
                    }
                    resolve(location);
                } else {
                    reject(new Error('解析地址失败'));
                }
            });
        });
    }

    async searchAddress(keyword) {
        return new Promise((resolve, reject) => {
            const localSearch = new BMapGL.LocalSearch(this.map, {
                onSearchComplete: (results) => {
                    if (localSearch.getStatus() === BMAP_STATUS_SUCCESS) {
                        const poi = results.getPoi(0);
                        if (poi) {
                            const point = poi.point;
                            this.updateLocation(point);
                            resolve(poi);
                        } else {
                            reject(new Error('未找到地址'));
                        }
                    } else {
                        reject(new Error('搜索失败'));
                    }
                }
            });
            localSearch.search(keyword);
        });
    }
} 